﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.Commands.CottagesCommands
{
    public class CottageUpdateHander : IRequestHandler<CottageUpdateCommand, Result>
    {

        private readonly IBaseRepository<Cottages> cottageRepository;
        private readonly ILogger<CottageUpdateHander> logger;

        public CottageUpdateHander(IBaseRepository<Cottages> cottageRepository, ILogger<CottageUpdateHander> logger)
        {
            this.cottageRepository = cottageRepository;
            this.logger = logger;
        }

        /// <summary>
        /// 修改栋舍信息
        /// </summary>
        /// <param name="request">修改栋舍信息请求对象</param>
        /// <param name="cancellationToken"></param>
        /// <returns>修改栋舍信息响应对象</returns>
        public async Task<Result> Handle(CottageUpdateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result();
                var info = cottageRepository.GetAll().FirstOrDefault(c => c.CottagesId == request.CottagesId);
                if (info != null)
                {
                    info.FarmId = request.FarmId;
                    info.CottageTypeId= request.CottageTypeId;
                    info.CottageName = request.CottageName;
                    info.PersId = request.PersId;
                    info.Production = request.Production;
                    info.Status = request.Status;
                    info.Remark = request.Remark;
                    info.UpdateId = request.UpdateId;
                    info.UpdateTime=DateTime.Now;
                }
                result.Code =  cottageRepository.Update(info);
                logger.LogInformation("修改栋舍信息方法调用成功");
                return result;

            }
            catch (Exception ex )
            {
                logger.LogError("修改栋舍信息方法调用出错" + ex);
                throw;
            }
        }
    }
}
